<?php
/**
 * Description of news
 *
 * @author Dmitry
 */
class Models_News extends Access_Db {
    function getStringOfNews($count)
    {
        $select = array(
            'id',
            'title',
            'body',
            'created',
            'modified',
            'user_id'
        );
        $table = $this->lang . '_articles';
        $where = "visible='1' and news='1' ORDER BY modified DESC LIMIT 0,$count";
        $this->select($select, $table, $where);
        $result  = array();
        while($row = $this->getSelected())
        {
            $row['body'] = strip_tags($row['body']);
            $row['body'] = $this->getMiniature($row['body'], $this->newsLength);
            $result[] = $row;
        }
        return $result;
    }
    
    function getNews($id)
    {
        $select = array(
            'title',
            'body',
            'created',
            'modified',
            'comments',
            'user_id',
            $this->lang . '_menu_id'
        );
        $table = $this->lang . '_articles';
        $this->select($select, $table, "id='$id' and visible=1  and news='1'");//TODO EXCEPTIONS !IMPORTANT
        $result = $this->getSelected();
        return $result;
    }
    
    function getAllNews()
    {
        $select = array(
            'title',
            'body',
            'created',
            'modified',
            'comments',
            'user_id',
            $this->lang . '_menu_id'
        );
        $table = $this->lang . '_articles';
        $this->select($select, $table, "visible=1 and news='1'");
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    //function return the first N symbols (dont break words)
    function getMiniature($string, $length)
    {
        if(strlen($string) <= $length)
        {
            return $string;
        }
        $result = substr($string, 0,strpos($string, " ", $length));
        $result .= '...';
        return $result;
    }
}